<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //promise.all简单原理
        let a = new Promise((resolve, reject) => {
            resolve('成功')
        })
        let b = new Promise((resolve, reject) => {
            reject('失败')
        })

        //promise.all原理
        function myPromise(list) {
            return new Promise((reslove, reject) => {
                const len = list.length
                let reslutArr = new Array(len)
                let num = 0
                list.forEach((item, index) => {
                    Promise.resolve(item).then((val) => {
                        num++
                        console.log(num)
                        reslutArr[index] = val
                        if (num === len) {
                            return reslove(reslutArr)
                        }
                    }, (reason) => {
                        return reject(reason)
                    })
                })
            })
        }
        myPromise([a, b]).then(res => {
            console.log(res)
        }).catch(err => {
            console.log(err)
        })
    </script>
</body>

</html>